﻿using UnityEngine;
using System.Collections;

public class BirdFlappingScript : MonoBehaviour {

	GameObject birdObject;
	Transform birdTransform;
	Animator birdAnimator;
	public float JumpForce = 1000.0f;
	public float JumpMax = 10000.0f;
	public float WalkForce = 100.0f;

	public float addForceTimer = 1.0f;
	public float gravitah = 4f;

	float jumpCoolDown = 0f;
	float jumpCoolDownStart = 0.0f;
	bool canClick = true;

	// Use this for initialization
	void Start () {
		Input.multiTouchEnabled = false;
		this.Reset ();
		//GameMaintainerSingleton.Instance.StartGame ();
	}

	public void Reset() {
		this.canClick = true;
		this.birdObject = GameObject.Find ("MainBird");
		if (this.birdObject == null) {
			Debug.LogError("Bird not found");
		}
		this.birdAnimator = this.birdObject.GetComponent<Animator> ();
		this.birdTransform = this.birdObject.transform;
		
		//Input.multiTouchEnabled = false;
		
	}

	void MoveLeft() {
		this.birdAnimator.SetBool ("IsFlapping", true);

		Vector3 scale = this.birdTransform.localScale;
		scale.x = -1.0f;
		this.birdTransform.localScale = scale;

		// This code makes it do a hard left turn
		//if (this.birdTransform.rigidbody2D.velocity.x > 0f) {
			Vector2 vel = this.birdTransform.rigidbody2D.velocity;
			vel.x = 0;
			this.birdTransform.rigidbody2D.velocity = vel;
		//}

		this.birdTransform.rigidbody2D.AddForce (new Vector2 (-1.0f * this.WalkForce, 0f));
	}

	void MoveRight() {
		this.birdAnimator.SetBool ("IsFlapping", true);

		Vector3 scale = this.birdTransform.localScale;
		scale.x = 1.0f;
		this.birdTransform.localScale = scale;

		// This code makes it do a hard right turn
		//if (this.birdTransform.rigidbody2D.velocity.x < 0f) {
			Vector2 vel = this.birdTransform.rigidbody2D.velocity;
			vel.x = 0;
			this.birdTransform.rigidbody2D.velocity = vel;
		//}
		
		this.birdTransform.rigidbody2D.AddForce (new Vector2 (this.WalkForce, 0f));
	}

	void Jump() {
		if (this.birdTransform.rigidbody2D.velocity.y < this.JumpMax) {
				
				//if (this.birdTransform.rigidbody2D.velocity.y < 0f) {
						Vector2 vel = this.birdTransform.rigidbody2D.velocity;
						vel.y = 0f;
						this.birdTransform.rigidbody2D.velocity = vel;
				//}
				this.birdTransform.rigidbody2D.AddForce (new Vector2 (0f, this.JumpForce));
				GameMaintainerSingleton.Instance.PlayFlapSound ();
				this.birdAnimator.SetBool ("IsFlapping", true);
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (Time.timeScale == 0.0f) {
			return;
		}

		if (this.jumpCoolDown <= 0f) {
			if (Input.GetKeyUp (KeyCode.Q)) {
					MoveLeft ();
				this.Jump ();
			} else if (Input.GetKeyUp (KeyCode.W)) {
					MoveRight ();
				this.Jump();
			}

			if (Input.GetKeyDown (KeyCode.Space)) {
					this.Jump ();
			}

			if (Input.GetMouseButtonDown (0)) {
				if (this.canClick) {
					canClick = false;
					int mousePosIntX = System.Convert.ToInt32 (Input.mousePosition.x);
					int halfScreen = Screen.width / 2;
					if (mousePosIntX < halfScreen) {
							MoveLeft ();
					} else {
							MoveRight ();
					}
					Jump ();
				}
			}
			else if (Input.GetMouseButtonUp(0)) {
				canClick = true;
			}
			else if (Input.touchCount >= 1) {
				Touch myTouch = Input.GetTouch (0);
				if (myTouch.phase == TouchPhase.Began) {
					//Debug.Log (myTouch.phase.ToString () + " " + Input.touchCount.ToString () + " " + myTouch.tapCount.ToString ());
					if (canClick) {
						canClick = false;
						int mousePosIntX = System.Convert.ToInt32 (myTouch.position.x);
						int halfScreen = Screen.width / 2;
						if (mousePosIntX < halfScreen) {
							MoveLeft ();
						} else {
							MoveRight ();
						}
						Jump ();
					}
				}
				else if (myTouch.phase == TouchPhase.Ended || myTouch.phase == TouchPhase.Canceled) {
					canClick = true;
				}
			}

			this.jumpCoolDown = this.jumpCoolDownStart;
		}
		else {
			this.jumpCoolDown -= Time.deltaTime;
		}
	}
}